package com.bishe.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.api.entitis.FileInfo;
import com.api.entitis.Resume;
import com.api.entitis.User;
import com.api.entitis.query.ResumeQuery;
import com.api.service.IFileService;
import com.api.service.IResumeService;
import com.api.service.IUserService;
import com.api.util.JobUtils;
import com.api.util.UserStpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.mapper.ResumeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 简历接口实现
 * @date 2022/02/17 08:02:07
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements IResumeService {

    @Autowired
    private IUserService userService;

    @Autowired
    private IFileService fileService;

    @Override
    public void  addOrUpdate(Resume resume) {
        //获取简历
        Resume resume1 = getResume();
        //创建简历
        if (resume1 == null){
            resume.setId(resume.getId());
            resume.setCreateBy(JobUtils.getLoginId());
            resume.setCreateTime(new DateTime());
            resume.setUpdateTime(new DateTime());
            save(resume);
            return;
        }
        Resume byId = getById(resume.getId());
        if (!resume1.getId().equalsIgnoreCase(byId.getId())){
            return;
        }
        //修改
        byId.setTitle(resume.getTitle());
        byId.setType(resume.getType());
        byId.setContent(resume.getContent());
        updateById(byId);
    }

    @Override
    public IPage<Resume> toPage(ResumeQuery query) {

        LambdaQueryWrapper<Resume> wrapper = new LambdaQueryWrapper<>();
        // 若搜索条件不为空
        if (!StrUtil.isEmpty(query.getType())){
            wrapper.like(Resume::getType,query.getType());
        }

        if (!StrUtil.isEmpty(query.getTitle())){
            wrapper.like(Resume::getTitle,query.getTitle());
        }
        // 倒叙
        wrapper.orderByDesc(Resume::getCreateTime);

        IPage<Resume> page = this.page(query.toPage(), wrapper);

        // 获取用户名
        List<Resume> records = page.getRecords();
        if (!CollUtil.isEmpty(records)){
            records.forEach(s->{
                List<FileInfo> byDataId = fileService.getByDataId(s.getId());
                if(CollUtil.isNotEmpty(byDataId)){
                    s.setFileInfo(byDataId.get(0));
                }
                String createBy = s.getCreateBy();
                s.setUserInfo(userService.getUser(createBy));
            });
        }
        return page;
    }

    @Override
    public Resume getResume() {
        String loginId = (String) UserStpUtil.getLoginId();
        Resume one = getOne(new LambdaQueryWrapper<Resume>().eq(Resume::getCreateBy, loginId), false);
        if (one== null){
            return null;
        }

        List<FileInfo> byDataId = fileService.getByDataId(one.getId());
        if(CollUtil.isNotEmpty(byDataId)){
            one.setFileInfo(byDataId.get(0));
        }
        String createBy = one.getCreateBy();
        one.setUserInfo(userService.getUser(createBy));

        return one;
    }

    @Override
    public Resume getDetail(String id) {

        Resume resume = this.getById(id);
        if (resume != null){
            String createBy = resume.getCreateBy();
            List<FileInfo> fileInfoDate = fileService.getByDataId(resume.getId());
            User user = userService.getUser(createBy);
            resume.setUserInfo(user);
            if (CollUtil.isNotEmpty(fileInfoDate)){
                resume.setFileInfo(fileInfoDate.get(0));
            }
        }
        return resume;
    }
}
